package com.asset.dispoal.file.evidenceRecognition.rar;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCaseDisposalFile;
import lombok.extern.slf4j.Slf4j;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

/**
 * 解压工具类
 *
 * @author: yf
 * @date: 2022-08-22 08:01:28
 */
@Slf4j
public class FileDecUtils {

    public static List<BsCaseDisposalFile> unRar(String originPath, String baseResolveUrl, String outIgnoreHeadPath, String generateId) {

        IInArchive inArchive;
        try (RandomAccessFile randomAccessFile = new RandomAccessFile(originPath, "r")) {
            inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));

            int[] in = new int[inArchive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            ExtractCallback extractCallback = new ExtractCallback(inArchive, baseResolveUrl, outIgnoreHeadPath, generateId);
            inArchive.extract(in, false, extractCallback);
            return extractCallback.getFileList();
        } catch (FileNotFoundException e) {
            log.error("找不到文件", e);
            return null;
        } catch (IOException e) {
            log.error("文件流问题", e);
            return null;
        }
    }
}
